package com.kaka.dao.daoimpl;

import com.kaka.dao.StudentDao;
import com.kaka.bean.Student;
import com.kaka.util.MyJDBCUtil;

import java.util.List;

public class StudentDaoImpl implements StudentDao {

    @Override
    public List<Student> selectByUsernameAndPassword(String username, String password) {
        String sql = "select * from student where username = ? and password = ?";
        return MyJDBCUtil.dqlSQL(Student.class, sql, username, password);
    }

    @Override
    public int updateTidBySid(int tid, int sid) {
        String sql = "update student set tid = ? where sid = ?";
        return MyJDBCUtil.dmlSQL(sql, tid, sid);
    }

    @Override
    public List<Student> selectBySid(int sid) {
        String sql = "select * from student where sid = ?";
        return MyJDBCUtil.dqlSQL(Student.class, sql, sid);
    }

    @Override
    public int updatePasswordBySid(int sid, String newPassword) {
        String sql = "update student set password = ? where sid = ?";
        return MyJDBCUtil.dmlSQL(sql, newPassword, sid);
    }

    @Override
    public List<Student> selectAllByTid(int tid) {
        String sql = "select * from student where tid = ?";
        return MyJDBCUtil.dqlSQL(Student.class, sql, tid);
    }

    @Override
    public int updateScoreBySid(int sid, double score) {
        String sql = "update student set score = ? where sid = ?";
        return MyJDBCUtil.dmlSQL(sql, score, sid);
    }

    @Override
    public List<Student> selectByTidAndStatus(int tid, int status) {
        String sql = "select * from student where tid = ? and status = ?";
        return MyJDBCUtil.dqlSQL(Student.class, sql, tid, status);
    }

    @Override
    public int updateStatusBySid(int sid, int status) {
        String sql = "update student set status = ? where sid = ?";
        return MyJDBCUtil.dmlSQL(sql, status, sid);
    }

    @Override
    public int updateStatusByTid(int tid, int status) {
        String sql = "update student set status = ? where tid = ?";
        return MyJDBCUtil.dmlSQL(sql, status, tid);
    }

    @Override
    public int insertNewStudent(Student student) {
        String sql = "insert into student (username,password,name) values (?,?,?)";
        return MyJDBCUtil.dmlSQL(sql, student.getUsername(), student.getPassword(), student.getName());
    }

    @Override
    public int deleteBySid(int sid) {
        String sql = "delete from student where sid = ?";
        return MyJDBCUtil.dmlSQL(sql, sid);
    }
}
